<?php 
class Kriteria {
	var $arrEntitas;  # bisa data Pelamar atau Kriteria
	var $arrHasil;
	var $arrKriteriaA;
	var $arrKriteriaB;
	var $arrKriteriaC;
	var $arrKriteriaD;
	var $arrKriteriaE;
	var $arrKriteriaF;
	var $arrKriteriaG;
	var $dataH;
	var $dataI;
	var $dataJ;
	var $jmlData;
	var $CR;
	var $RI;
	var $DB;
	var $link;
	
	public $idTes;
	
	function Kriteria() {
		$this->arrEntitas = array();
		$this->arrHasil = array();
		$this->arrKriteriaA = array();
		$this->arrKriteriaB = array();
		$this->arrKriteriaC = array();
		$this->arrKriteriaD = array();
		$this->arrKriteriaE = array();
		$this->arrKriteriaF = array();
		$this->arrKriteriaG = array();
		$this->dataH = 0;
		$this->dataI = 0;
		$this->dataJ = 0;
		$this->CR = 0.1;
		$this->RI = array(0, 0.0, 0.0, 0.58, 0.9, 1.12, 1.24, 1.32, 1.41, 1.45, 1.49);
		$this->DB = 'sppk_kanisius';
	}
	
	function ABacaData($namaFile) {
		# baca data dari file dan dimasukkan ke array "arrKriteriaA" sebagai data awal
		$data = fopen($namaFile, 'r');
		$r = 0;
		
		#$file = file_get_contents('kriteria.csv');
		#print $file;
		
		while (!feof($data)) {
			$baris = fgets($data);
			$kolom = explode(",", $baris);
			$jmlKolom = count($kolom);
			
			if ($r <= $jmlKolom) {
				$this->arrKriteriaA[$r] = $kolom;
				$r++;
				#print "<br/> $baris <br/>";
			}
		}
		
		fclose($data);
		$this->jmlData = count($this->arrKriteriaA)-1;
		
		# simpan data pelamar
		for ($r = 1; $r <= $this->jmlData; $r++) {
			$this->arrEntitas[$r] = $this->arrKriteriaA[$r][0];
		}
		
		print "==================== <br/>";
		print "=== OUTPUT SECTION A <br/>";
		print "==================== <br/>";
		$this->PrintHTMLTabel($this->arrKriteriaA);
	}
	
	function BHitungBobot() {
		# perhitungan bobot dengan membagi setiap entri dengan jumlah total setiap kolom
		$arrTotKolom = array();
		$arrTotKolom[0][0] = '';
		
		# hitung total tiap kolom		
		for ($c = 1; $c <= $this->jmlData; $c++) {
			$totKolom = 0;
			
			for ($r = 1; $r <= $this->jmlData; $r++) {
				$totKolom = $totKolom + $this->arrKriteriaA[$r][$c];
			}
			
			$arrTotKolom[0][$c] = $this->arrKriteriaA[0][$c];
			$arrTotKolom[1][0] = 'Jumlah';
			$arrTotKolom[1][$c] = round($totKolom, 3);
		}
		
		print "<br/>";
		print "=== TOTAL PER KOLOM <br/>";
		$this->PrintHTMLTabel($arrTotKolom);
		$this->arrKriteriaB[0][0] = '';
		
		# hitung bobot, set presisi ke 3 digit		
		for ($c = 1; $c <= $this->jmlData; $c++) {
			$this->arrKriteriaB[0][$c] = $this->arrKriteriaA[0][$c];
			
			for ($r = 1; $r <= $this->jmlData; $r++) {
				$this->arrKriteriaB[$r][0] = $this->arrKriteriaA[$r][0];
				$this->arrKriteriaB[$r][$c] = round($this->arrKriteriaA[$r][$c] / $arrTotKolom[1][$c], 3);
			}
		}
		
		print "<br/>";
		print "==================== <br/>";
		print "=== OUTPUT SECTION B <br/>";
		print "==================== <br/>";
		$this->PrintHTMLTabel($this->arrKriteriaB);
	}
	
	function CJumlahKanan() {
		$this->arrKriteriaC[0][0] = '';
		
		for ($r = 1; $r <= $this->jmlData; $r++) {
			$totBaris = 0;
			
			for ($c = 1; $c <= $this->jmlData; $c++) {
				 $totBaris = $totBaris + $this->arrKriteriaB[$r][$c];
			}
			
			$this->arrKriteriaC[$r][0] = $this->arrKriteriaA[$r][0];
			$this->arrKriteriaC[0][1] = 'Jumlah';
			$this->arrKriteriaC[$r][1] = $totBaris;			
		}
		
		print "<br/>";
		print "==================== <br/>";
		print "=== OUTPUT SECTION C <br/>";
		print "==================== <br/>";
		$this->PrintHTMLTabel($this->arrKriteriaC);
	}
	
	function DHitungVektor() {
		$this->arrKriteriaD[0][0] = '';
		
		for ($r = 1; $r <= $this->jmlData; $r++) {
			$this->arrKriteriaD[$r][0] = $this->arrKriteriaA[$r][0];
			$this->arrKriteriaD[0][1] = 'Vektor Prioritas';
			$this->arrKriteriaD[$r][1] = round($this->arrKriteriaC[$r][1] / $this->jmlData, 3);
		}
		
		print "<br/>";
		print "==================== <br/>";
		print "=== OUTPUT SECTION D <br/>";
		print "==================== <br/>";
		$this->PrintHTMLTabel($this->arrKriteriaD);
	}
	
	function EKonsistensi() {
		$this->arrKriteriaE[0][0] = '';
		
		for ($r = 1; $r <= $this->jmlData; $r++) {
			$this->arrKriteriaE[$r][0] = $this->arrKriteriaA[$r][0];
			
			for ($c = 1; $c <= $this->jmlData; $c++) {
				$this->arrKriteriaE[0][$c] = $this->arrKriteriaA[0][$c];
				$this->arrKriteriaE[$r][$c] = round($this->arrKriteriaA[$r][$c] * $this->arrKriteriaD[$c][1], 3);
			}
		}
		
		print "<br/>";
		print "==================== <br/>";
		print "=== OUTPUT SECTION E <br/>";
		print "==================== <br/>";
		$this->PrintHTMLTabel($this->arrKriteriaE);
	}
	
	function FJumlahKanan() {
		$this->arrKriteriaF[0][0] = '';
		
		for ($r = 1; $r <= $this->jmlData; $r++) {
			$totBaris = 0;
			
			for ($c = 1; $c <= $this->jmlData; $c++) {
				 $totBaris = $totBaris + $this->arrKriteriaE[$r][$c];
			}
			
			$this->arrKriteriaF[$r][0] = $this->arrKriteriaA[$r][0];
			$this->arrKriteriaF[0][1] = 'Jumlah';
			$this->arrKriteriaF[$r][1] = $totBaris;
		}
		
		print "<br/>";
		print "==================== <br/>";
		print "=== OUTPUT SECTION F <br/>";
		print "==================== <br/>";
		$this->PrintHTMLTabel($this->arrKriteriaF);
	}
	
	function GBagiPrioritas() {
		$this->arrKriteriaG[0][0] = '';
		
		for ($r = 1; $r <= $this->jmlData; $r++) {
			$this->arrKriteriaG[$r][0] = $this->arrKriteriaA[$r][0];
			$this->arrKriteriaG[0][1] = 'Jumlah';
			$this->arrKriteriaG[$r][1] = round($this->arrKriteriaF[$r][1] / $this->arrKriteriaD[$r][1], 3);
		}
		
		print "<br/>";
		print "==================== <br/>";
		print "=== OUTPUT SECTION G <br/>";
		print "==================== <br/>";
		$this->PrintHTMLTabel($this->arrKriteriaG);
	}
	
	function HLambda() {
		$total = 0;
		
		for ($r = 1; $r <= $this->jmlData; $r++) {
			$total = $total + $this->arrKriteriaG[$r][1];
		}
		
		$this->dataH = round($total / $this->jmlData, 3);
		print "<br/>";
		print "==================== <br/>";
		print "=== OUTPUT SECTION H <br/>";
		print "==================== <br/>";
		print("Lambda maks.: <b>$this->dataH</b>");
		print "<br/>";
	}
	
	function IHitungCI() {
		$this->dataI = round(($this->dataH - $this->jmlData) / ($this->jmlData - 1), 3);
		print "<br/>";
		print "==================== <br/>";
		print "=== OUTPUT SECTION I <br/>";
		print "==================== <br/>";
		print("CI: <b>$this->dataI</b>");
		print "<br/>";
	}
	
	function JHitungCR() {
		$this->dataJ = round(($this->dataI / $this->RI[$this->jmlData]), 3);
		print "<br/>";
		print "==================== <br/>";
		print "=== OUTPUT SECTION J <br/>";
		print "==================== <br/>";
		print("CR: <b>$this->dataJ</b>");
		print "<br/>";
	}
	
	function KCekCR() {
		print "<br/>";
		print "===================== <br/>";
		print "=== OUTPUT KESIMPULAN <br/>";
		print "===================== <br/>";
		
		if ($this->dataJ <= $this->CR) {
			print "Nilai CR = <b>$this->dataJ</b> berarti nilai CR <= $this->CR, maka <b>Konsistensi Diterima</b>";
		} else {
			print "Nilai CR = <b>$this->dataJ</b> berarti nilai CR > $this->CR, maka <b>Konsistensi Tidak Diterima</b>";
		}
		
		print "<br/>";
	}
	
	function KonekDB() {
		$this->link = mysql_connect('localhost', 'root', ''); 
		
		if (!$this->link) { 
			die('Autentikasi MySQL gagal: ' . mysql_error()); 
			return false;
		} else {
			$db_selected = mysql_select_db($this->DB, $this->link);
			
			if (!$db_selected) {
    		die("Tidak bisa membuat koneksi ke $this->DB: " . mysql_error());
			}
		}
	}
	
	function CekNamaTes() {
		$this->KonekDB();
		$query = sprintf("SELECT nmTes FROM tes WHERE idTes = '%s'", $this->idTes);
		$rs = mysql_query($query, $this->link);
		$data = mysql_fetch_array($rs);
		
		return $data['nmTes'];
	}
	
	function SimpanHasil() {
		$this->KonekDB(); 
		$this->arrHasil[0][0] = '';
		$this->arrHasil[0][1] = 'Hasil';
				
		for ($r = 1; $r <= $this->jmlData; $r++) {
			$this->arrHasil[$r][0] = $this->arrEntitas[$r];
			$this->arrHasil[$r][1] = $this->arrKriteriaE[$r][$r];
			
			$query = sprintf("SELECT nama FROM nilaites WHERE idTes = %d AND LOWER(nama) = LOWER('%s')", 
				$this->idTes, $this->arrEntitas[$r]);
			$rs = mysql_query($query, $this->link);
			$data = mysql_fetch_array($rs);
			
			if (!empty($data)) {
				$query = sprintf("UPDATE nilaites SET nilai = %f WHERE idTes = %d AND LOWER(nama) = LOWER('%s')", 
					$this->arrKriteriaE[$r][$r], $this->idTes, $this->arrEntitas[$r]);
				$op = mysql_query($query, $this->link);
			} else {
				$query = sprintf("INSERT INTO nilaites VALUES ('%s', %d, %f)", 
					$this->arrEntitas[$r], $this->idTes, $this->arrKriteriaE[$r][$r]);
				$op = mysql_query($query, $this->link);
			}
		}
		
		print "<br/>";
		print "==================== <br/>";
		print "=== OUTPUT HASIL TES <br/>";
		print "==================== <br/>";
		$this->PrintHTMLTabel($this->arrHasil);
		$namaTes = $this->CekNamaTes();
		
		print "<br/>";
		print "Berhasil menyimpan hasil perhitungan TES $namaTes ke database $this->DB <br/>";
	}
	
	function SimpanKriteria() {
		$this->KonekDB();
		$this->arrHasil[0][0] = '';
		$this->arrHasil[0][1] = 'Hasil';
		
		for ($r = 1; $r <= $this->jmlData; $r++) {
			$this->arrHasil[$r][0] = $this->arrEntitas[$r];
			$this->arrHasil[$r][1] = $this->arrKriteriaE[$r][$r];
		
			$query = sprintf("UPDATE tes SET bobot = %f WHERE LOWER(nmTes) = LOWER('%s')", 
				$this->arrKriteriaE[$r][$r], $this->arrEntitas[$r]);
			$op = mysql_query($query, $this->link);
		}
		
		print "<br/>";
		print "==================== <br/>";
		print "=== OUTPUT KRITERIA  <br/>";
		print "==================== <br/>";
		$this->PrintHTMLTabel($this->arrHasil);
		
		print "<br/>";
		print "Berhasil menyimpan hasil perhitungan KRITERIA ke database $this->DB <br/>";
	}
	
	function HasilAkhir() {
		$arrTmp = array();
		$arrNilaiTes = array();
		$this->KonekDB();
		
		$query = "SELECT count(*) FROM pelamar";
		$rs = mysql_query($query, $this->link);
		$jmlPelamar = mysql_fetch_array($rs);
		$jmlPelamar = $jmlPelamar[0];
		
		$query = "SELECT count(*) FROM tes";
		$rs = mysql_query($query, $this->link);
		$jmlTes = mysql_fetch_array($rs);
		$jmlTes = $jmlTes[0];
		
		print "<br/>";
		print "==================== <br/>";
		print "=== NILAI PER TES    <br/>";
		print "==================== <br/>";
		$query = "SELECT n.nama, t.nmtes, (n.nilai * t.bobot) AS total 
			FROM nilaites n, tes t, pelamar p 
			WHERE n.idtes = t.idtes AND n.nama = p.nama
			ORDER BY n.nama, t.idtes";
		$rs = mysql_query($query, $this->link);
		$i = 0;
		$j = 0;
		$arrNilaiTes[0][0] = 'Nama';
		
		while ($data = mysql_fetch_array($rs)) {
			$j++;
			
			if ($i == 0) {
				$arrNilaiTes[0][$j] = $data['nmtes'];
			}
			
			$arrNilaiTes[$i+1][0] = $data['nama'];
			$arrNilaiTes[$i+1][$j] = round($data['total'], 3);
			
			if ($j == $jmlTes) {
				$j = 0;
				$i++;
			}
		};		
		
		$this->PrintHTMLTabel($arrNilaiTes);
		
		print "<br/>";
		print "==================== <br/>";
		print "=== PERINGKAT        <br/>";
		print "==================== <br/>";
		$query = "SELECT n.nama, SUM(n.nilai * t.bobot) AS total 
			FROM nilaites n, tes t, pelamar p 
			WHERE n.idtes = t.idtes AND n.nama = p.nama
			GROUP BY n.nama
			ORDER BY total DESC";
		$rs = mysql_query($query, $this->link);
		$i = 0;
		$arrTmp[0][0] = 'Peringkat';
		$arrTmp[0][1] = 'Nama';
		$arrTmp[0][2] = 'Nilai';
		
		while ($data = mysql_fetch_array($rs)) {
			$i++;
			$arrTmp[$i]['urut'] = $i;
			$arrTmp[$i]['nama'] = $data['nama'];
			$arrTmp[$i]['total'] = round($data['total'], 3);
		};
		
		$this->PrintHTMLTabel($arrTmp);
		
		for ($r = 1; $r <= $i; $r++) { 
			$query = sprintf("UPDATE pelamar SET nilai = %f, peringkat = %d 
			  WHERE nama = '%s'", $arrTmp[$r]['total'], $r, $arrTmp[$r]['nama']);			
			$rs = mysql_query($query, $this->link);
		}
		
		print "<br/>";
		print "Berhasil menyimpan hasil tes tabel PELAMAR <br/>";
	}
	
	function PrintHTMLTabel($data) {
		$table = '';
		$table .= '<table border="1">';
		$nobaris = 0;
		
		foreach ($data as $baris) {
			$table .= '<tr>';
			$nokolom = 0;
			
			if (is_array($baris)) {
				foreach ($baris as $kolom) {
					if ($nokolom == 0) {
						$table .= '<th>';
						$table .= $kolom;
						$table .= '</th>';
					} else {
						$table .= '<td>';
						$table .= $kolom;
						$table .= '</td>';
					};
				}
				
				$nokolom++;
			} else {
				if ($nobaris == 0) {
					$table .= '<th>';
					$table .= $baris;
					$table .= '</th>';
				} else {
					$table .= '<td>';
					$table .= $baris;
					$table .= '</td>';
				};
					
				$nobaris++;
			}
			
			$table .= '</tr>';
		}
		
		$table .= '</table>';
		echo $table;
		#var_dump($data);
	}
}
?> 